<?php
namespace c\file;
/**
 * Description of file
 *
 * @author YeFng
 */
class a extends \m\ad
{
    var $class = array('F'=>'\files');
    var $auth_m = 'file';
    
    /**
     * 浏览
     */
    function index_home()
    {
        $this->PU->login();
        $this->loadTemplate('file/home');
        if (isset($_GET['select']))
            $this->dispLayout = 'sub';
    }
    //目录
    function index_dir()
    {   
        $this->PU->login();
    	if ($_REQUEST['dir'])
         {
    	 	$baseDir = $this->F->ok_path(FILE_DIR,  $_REQUEST['dir']);
            if (!$baseDir)
                d(\lang::g ('NO'));
            $pre = str_replace('\\','/', $this->F->charset_p($baseDir));
         }
        else{
           $baseDir = $pre = ''; 
         }
         
        $res = array();
        foreach ($this->F->dirList( FILE_DIR.$baseDir) as $v)
            $res[] = array('id'=>$pre . $v, 'name'=>$v, 'subNum'=>'1');       
        ajax_json(array('status'=>TRUE, 'res'=>$res));
    }
	// 文件列表   
    function index_list()
    {	
		
        if ($this->PU->check($this->auth_m, 1))
			$like = '*';
		else
			$like =  \YF_f::$login_user_id . '_*';
			
		if ($_GET['dir'])
       	 {
       	 	$dir = $this->F->ok_path(FILE_DIR, $_GET['dir']);
       	 	$pre = str_replace('\\', '/', $this->F->charset_p($dir));
       	 }
       	else
       		$dir = $pre = '';
        
		$res = $this->F->getList( FILE_DIR. $dir,  $pre, $like);
		ajax_json(array('status'=>TRUE, 'res'=>$res));
    }
    
    /**
     * 编辑
     */
    function index_upload()
    {
        $this->PU->login();
        if ($_GET['baseDir'])
            $baseDir = $this->F->ok_path(FILE_DIR, $_GET['baseDir']);

        if ($_GET['dir'])
            $dir = $this->F->ok_path(FILE_DIR.$baseDir, $_GET['dir']);
        else
            $dir = $baseDir . date('Y/m/');
			
        $r = \YF_f::initC('\c\file\upload')->ing($_FILES['file'], $dir);
		ajax_json($r, false);
    }
	// 
	function index_up_CKEditor()
	{
        $dir = 'info/' . date('Y/m/');	
        $r = \YF_f::initC('\c\file\upload')->ing($_FILES['upload'], $dir);
		
		$funcNum = intval($_GET['CKEditorFuncNum']);
		$url = addslashes(\url::pic($r['path']));
		$this->clue(\lang::g('OK'), "javascript:window.parent.CKEDITOR.tools.callFunction($funcNum, '$url', '$message');", 0);
	}
    
    //del
    function index_del()
    {
        $this->PU->check($this->auth_m);
        $f = $this->F->ok_path(FILE_DIR, $_GET['f']);
        if ($f){
            \log::write('file', 'del', $f);
            $b = is_file(FILE_DIR.$f) ? unlink(FILE_DIR.$f) : $this->F->del_dir(FILE_DIR.$f);
            if ($b)
                ajax_json(TRUE);
        }
        ajax_json(\lang::g('Over'));
    }
    
    // 创建目录
    function index_foundDir()
    {
        $this->PU->check($this->auth_m);
    	$baseDir = $_GET['dir'] ? $this->F->ok_path(FILE_DIR,  $_GET['dir']): '';
        $name = realFilename($_POST['name']);
        $name = $this->F->charset_sys($name);
        $b = $this->F->c_dir( FILE_DIR . $baseDir . $name);
        ajax_json($b ?TRUE :'Over');
    }
}
